package com.lzjn.code.web.service.impl;

import com.lzjn.code.common.base.BaseDao;
import com.lzjn.code.common.constant.Constants;
import com.lzjn.code.common.model.PageModel;
import com.lzjn.code.common.util.PKUtil;
import com.lzjn.code.web.dao.TableConfigDao;
import com.lzjn.code.web.dao.TableFieldConfigDao;
import com.lzjn.code.web.entity.TableConfig;
import com.lzjn.code.web.service.TableConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;

@Service("TableConfigService")
public class TableConfigServiceImpl implements TableConfigService {

	private static Logger log = LoggerFactory.getLogger(TableConfigServiceImpl.class);
	
	@Autowired
	private TableConfigDao tableConfigDao;
	@Autowired
	private TableFieldConfigDao tableFieldConfigDao;
	
	
	@Override
	public PageModel getByPage(TableConfig tableConfig, PageModel page) {
		
		return tableConfigDao.getByPage(tableConfig, page);
	}


	@Override
	public TableConfig getById(String id) {
		
		TableConfig tableConfig = tableConfigDao.getById(id);
		tableConfig.setFieldConfigs(tableFieldConfigDao.getList(id));
		
		return tableConfig;
	}
	
	@Override
	public String add(TableConfig tableConfig) throws Exception {
		
		Connection conn = BaseDao.getConnection();
		
		try {
			
			conn.setAutoCommit(false);
			
			tableConfig.setId(PKUtil.getUUID());
			
			tableConfigDao.add(tableConfig, conn);
			tableFieldConfigDao.batchAdd(tableConfig, conn);
			
			conn.commit();
			
		} catch (Exception e) {
			
			try {
				conn.rollback();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
			log.error(e.getMessage(), e);
			throw e;
		} finally {
			
			try {
				
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
		return Constants.MESSAGE_BUILD_CONFIG_SUCCESS;
	}



	@Override
	public String update(TableConfig tableConfig) throws Exception {
		
		if(tableConfigDao.getById(tableConfig.getId()) != null) {
			
			Connection conn = BaseDao.getConnection();
			
			try {
				
				conn.setAutoCommit(false);
				
				tableConfigDao.update(tableConfig, conn);
				
				tableFieldConfigDao.delete(tableConfig.getId(), conn);
				
				tableFieldConfigDao.batchAdd(tableConfig, conn);
				
				conn.commit();
				
			} catch (Exception e) {
				
				try {
					conn.rollback();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				}
				log.error(e.getMessage(), e);
				throw e;
			} finally {
				
				try {
					
					conn.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			
			return Constants.MESSAGE_SAVE_SUCCESS;
		}
		else {
			
			return Constants.MESSAGE_DATA_NOT_EXIST;
		}
	}



	@Override
	public String delete(String ids) throws Exception {
		
		boolean isExist = true;
		
		if(ids.contains(",")) {
			
			int existNum = tableConfigDao.getByIds(ids).size();
			
			int selectNum = ids.split(",").length;
			
			isExist = existNum == selectNum ? true : false;
		}
		else {
			
			isExist = tableConfigDao.getById(ids) != null ? true : false;
		}
		
		if(isExist) {
			
			Connection conn = BaseDao.getConnection();
			
			try {
				
				conn.setAutoCommit(false);
				
				if(ids.contains(",")) {
					
					tableConfigDao.batchDelete(ids, conn);
					tableFieldConfigDao.batchDelete(ids, conn);
				}
				else {
					
					tableConfigDao.delete(ids, conn);
					tableFieldConfigDao.delete(ids, conn);
				}
				
				conn.commit();
				
			} catch (Exception e) {
				
				try {
					conn.rollback();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				}
				log.error(e.getMessage(), e);
				throw e;
			} finally {
				
				try {
					
					conn.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			
			return Constants.MESSAGE_DELETE_SUCCESS;
		}
		else {
			
			return Constants.MESSAGE_DATA_NOT_EXIST;
		}
	}

}
